/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Installer.

    Maxsi Installer is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Installer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Installer.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiInstallerContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiInstaller.exe
	The main installing technology utilized in the powerful Maxsi Distribution
	platform.

	PageAgreementMaxsiDistribution.cpp
	The first page shown when installing

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Main.h" 
#include "UI.h" 
#include "Thread.h"
#include "Data.h"


bool PageAgreementMaxsiDistribution::InitializeForm(ME_Window* Parent)
{
	BaseClass::InitializeForm(Parent);

	RECT ClientSpace;
	GetClientRect (WindowHWND, &ClientSpace);

	Previous	=	CreateButton(WindowHWND,_MESTR("Previous"),0,0,0,0);
	Next		=	CreateButton(WindowHWND,_MESTR("I agree"),0,0,0,0);
	License		=	CreateTextExA(WindowHWND,MDLicense,0,0,0,0,WS_VISIBLE | WS_CHILD | ES_LEFT | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE);
	Text		=	CreateLabelA(WindowHWND,"Please carefully read Maxsi Distribution's License Agreement.",0,0,0,0);

	NewResolution((WORD)ClientSpace.right,(WORD)ClientSpace.bottom);

	return true;
}

bool PageAgreementMaxsiDistribution::RenderForm(HWND hWnd)
{
	return	BaseClass::RenderForm(hWnd);
}

LRESULT PageAgreementMaxsiDistribution::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (!Initialized) { return 0; }

	if ( message == WM_COMMAND )
	{
        switch(LOWORD(wParam)) 
        {
            case BN_CLICKED:
			{
				HWND ClickedButton = (HWND)lParam;
				if (ClickedButton == Previous)
				{
					if ( IngameStats )	ReplaceSelfWith( new PageAgreementMaxsiDistributionNotice );
					else				ReplaceSelfWith( new PageWelcome );
				}
				else if (ClickedButton == Next)
				{
					if ( bLicense )		ReplaceSelfWith( new PageAgreementProduct );
					else				ReplaceSelfWith( new PageWhereToInstall );
				}
			}				
            break;
			default:
			{
				return BaseClass::WindowProc(hWnd,message,wParam,lParam);
			}
		}
	}
	else
	{
		return BaseClass::WindowProc(hWnd,message,wParam,lParam);
	}
    return (0);

}


bool PageAgreementMaxsiDistribution::TerminateForm()
{
	// Avoid calling this function twice, or worse,
	// if one of the functions called here invokes another
	// windows message that eventually leads to this point of code again.
	if (!Initialized) { return false; }
	Initialized	=	false;

	DestroyWindow(Previous);
	DestroyWindow(Next);
	DestroyWindow(License);
	DestroyWindow(Text);

	// If we are here, we are safe. Call the parent and ask him to continue our work.
	// Make sure he sets Initialized to false afterwards.
	Initialized	=	true;
	BaseClass::TerminateForm();
	return true;
}

bool PageAgreementMaxsiDistribution::NewResolution( WORD Width, WORD Height)
{
	BaseClass::NewResolution(Width,Height);

	// Move the window, validate the Window, redraw the window	
	MoveWindow(Previous,Width-2*125,Height-2*24,100,24,TRUE);
	MoveWindow(Next,Width-1*125,Height-2*24,100,24,TRUE);
	MoveWindow(License		,25,80,Width-25-25,Height-80-70,TRUE);
	MoveWindow(Text,25,60,Width-25-25,80-60,TRUE);

	return true;
}